import {get_token,set_token,removeToken} from '../../cookes/token'
import {login,getInfo,logout} from '../../api/login'
import {Message} from 'element-ui'
import Cookies from 'js-cookie'
var storage=window.localStorage
const user = {
  state: {
    status: '',
    token: get_token(),
    username: '',
    headImage:'',
    roles: [],
    isLogin:''
  },
  mutations: {
    SET_TOKEN(state, token) {
      state.token = token
    },
    SET_NAME(state, username) {
      state.username = username
    },
    SET_ROLES(state, roles) {
      state.roles = roles
    },
    SET_HEADIMAGE(state, headImage) {
      state.headImage = headImage
    },
    SET_STATUS(state, status) {
      state.status = status
    },
    SET_ISLOGIN(state, isLogin) {
      state.isLogin = isLogin
    }
  },
  actions: {
    //登录操作
    Login(context, userInfo) { 
      var username = userInfo.username.trim()
      var password = userInfo.password.trim()
      var params = {
        username: username,
        password: password
      }
      return new Promise((resolve, reject) => {
        var url = 'http://localhost:3000/login'
        login(url, params).then(res => {
          const data = res.data
          set_token(data.token) //获取token存入cookie  结构：{X-TOKEN:token}
          context.commit('SET_TOKEN', data.token)
          resolve()
        })
      }).catch(error => {
        reject(error)
      })
    },
    //获取用户信息
    GetUserInfo(context){
      var url = 'http://localhost:3000/douban/getUserInfo'
      return new Promise((resolve, reject) => {
        getInfo(url,context.state.token).then(res=>{
          res = res.data
          var username = res.username
          var headImage = res.userImage 
          var roles = res.roles
          var status = res.status
          storage.setItem('userInfo',JSON.stringify({ username, roles, status, headImage})) 
          Cookies.set('userInfo',JSON.stringify({ username, roles, status, headImage}))
          context.commit('SET_NAME', username)
          context.commit('SET_ROLES', roles)
          context.commit('SET_STATUS', status)
          context.commit('SET_HEADIMAGE', headImage)
          let isLogin = true
          context.commit('SET_ISLOGIN', isLogin)
          console.log(context.state.username)
          
          resolve(res)
        })
      })
    },
    //后端登出
    LoginOut(context) { 
      var url = 'http://localhost:3000/loginout'
      return new Promise((resolve, reject) => {
        logout(url, {
            token: context.state.token
          })
          .then(res => {
            let msg = res.data.msg
            if (msg == 'success') {
              Message({
                message: '已注销',
                type: 'message',
              })
              context.commit('SET_ROLES', []) //删除用户权限
              removeToken() //删除cookie中的x-token
              
              resolve()
            }
          })
      }).catch(error => {
        reject(error)
      })
    },

    // 前端登出
    FedLogOut(context) {
      return new Promise(resolve => {
        //删除cookie中token
        removeToken()
        context.commit('SET_TOKEN', '')
        context.commit('SET_NAME', '')
        context.commit('SET_STATUS', '')
        context.commit('SET_ISLOGIN', false)
        context.commit('SET_ROLES', []) 
        //删除localStorage中userinfo
        storage.removeItem('userInfo')
        //删除cookie
        Cookies.remove('userInfo')
        Cookies.remove('username')
        Cookies.remove('password')
        resolve()
      })
    }
  }
}


export default user
